var preorderTraversal = function (root) {
    //递归法
    //return root?[root.val, ...preorderTraversal(root.left),...preorderTraversal(root.right)]:[];

    //迭代法 用数组模拟栈 先放右节点，后放左节点
    let list = [];
    let stack = [];
    if (!root) return [];
    root && stack.push(root)
    while (stack.length > 0) {
        let cur = stack.pop();
        list.push(cur.val);

        cur.right && stack.push(cur.right);
        cur.left && stack.push(cur.left)
    }
    return list;


};